package com.sepexam.trainsys.controller;

import com.sepexam.trainsys.entity.AjaxResult;
import com.sepexam.trainsys.entity.TrainInfo;
import com.sepexam.trainsys.service.TrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("train")
public class TrainController {

    @Autowired
    private TrainService trainService;

    //查询所有车次信息，并返回一个结果集
    @RequestMapping("/getAllTrain")
    @ResponseBody
    public AjaxResult getAllTrain() {
        List<TrainInfo> list=trainService.getAllTrain();
        return AjaxResult.success(list);
    }
    //删除车次信息
    @ResponseBody
    @RequestMapping("/delTrain")
    public AjaxResult delTrain(String trainId){
        String[] str = trainId.split(",");
        System.err.println("str:"+ Arrays.toString(str));
        return AjaxResult.success(trainService.delTrain(str));
    }
    //查询指定车次的车次信息
    @RequestMapping("/findTrain/{no}")
    public String findTrain(@PathVariable String no, Model model){
        model.addAttribute("TrainInfo",trainService.findTrain(no));
        return "trainDetail";
    }
}
